package logicaPresentacion;


import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import logicaNegocio.Alumno;


public class VerAlumno extends JInternalFrame implements ActionListener
{
    /**
	 * 
	 */

	private static final long serialVersionUID = -6733265360484493713L;
	private JLabel jlbNombre;       
	private JTextField  jtfNombre;
	private JLabel jlbApellidos;   
	private JTextField  jtfApellidos;
	private JLabel jlbDni;   		
	private JTextField  jtfDni;
	private JLabel jlbEdad;        
	private JTextField  jtfEdad;
	private JLabel jlbDireccion;   
	private JTextField  jtfDireccion;
	private JLabel jlbTelefono;    
	private JTextField  jtfTelefono;
    private Box boxVCamposVentana;       
	private JLabel picture, avatar;
	private boolean volver = false;
	private JButton  aceptar,mostrarAlumnos;
	
    public VerAlumno (Alumno a, String eleccion)
    {
    	super("Datos personales del alumno");

        setSize(400,650);
        Box boxVContenidoVerticalVentana = Box.createVerticalBox();
 
        Box boxHNombre =  Box.createHorizontalBox();
        jlbNombre = new JLabel("Nombre ");   
        jtfNombre = new JTextField(15);
        jtfNombre.setText(a.getNombre());
        jtfNombre.setEditable(false);
        boxHNombre.add(Box.createHorizontalStrut(10));
        boxHNombre.add(jlbNombre);
        boxHNombre.add(Box.createHorizontalStrut(20));
        boxHNombre.add(jtfNombre);
        JPanel panNombre =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panNombre.add(boxHNombre);

        Box boxHApellidos =  Box.createHorizontalBox();
        jlbApellidos = new JLabel("Apellidos ");   
        jtfApellidos = new JTextField(15);
        jtfApellidos.setText(a.getApellido());
        jtfApellidos.setEditable(false);
        
        boxHApellidos.add(Box.createHorizontalStrut(10));
        boxHApellidos.add(jlbApellidos);
        boxHApellidos.add(Box.createHorizontalStrut(11));
        boxHApellidos.add(jtfApellidos);
        JPanel panApellidos =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panApellidos.add(boxHApellidos);
        
        Box boxHDni =  Box.createHorizontalBox();
        jlbDni = new JLabel("Dni");   
        jtfDni = new JTextField(15);
        jtfDni.setText(a.getDni());
        jtfDni.setEditable(false);
        boxHDni.add(Box.createHorizontalStrut(10));
        boxHDni.add(jlbDni);
        boxHDni.add(Box.createHorizontalStrut(11));
        boxHDni.add(jtfDni);
        JPanel panDni =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panDni.add(boxHDni);

        Box boxHEdad =  Box.createHorizontalBox();
        jlbEdad = new JLabel("Edad");   
        jtfEdad = new JTextField(15);
        jtfEdad.setText(Integer.toString(a.getEdad()));
        jtfEdad.setEditable(false);
        
        boxHEdad.add(Box.createHorizontalStrut(10));
        boxHEdad.add(jlbEdad);
        boxHEdad.add(Box.createHorizontalStrut(40));
        boxHEdad.add(jtfEdad);
        JPanel panEdad =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panEdad.add(boxHEdad);
        
        Box boxDireccion =  Box.createHorizontalBox();
        jlbDireccion = new JLabel("Direccion ");  
        jtfDireccion = new JTextField(15);
        jtfDireccion.setText(a.getDireccion());
        jtfDireccion.setEditable(false);
        boxDireccion.add(Box.createHorizontalStrut(10));
        boxDireccion.add(jlbDireccion);
        boxDireccion.add(Box.createHorizontalStrut(40));
        boxDireccion.add(jtfDireccion);
        JPanel panDireccion =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panDireccion.add(boxDireccion);
        
        Box boxTelefono =  Box.createHorizontalBox();
        jlbTelefono = new JLabel("Telefono");   
        jtfTelefono = new JTextField(15);
        jtfTelefono.setText(Integer.toString(a.getTelefono()));
        jtfTelefono.setEditable(false);
        boxTelefono.add(Box.createHorizontalStrut(10));
        boxTelefono.add(jlbTelefono);
        boxTelefono.add(Box.createHorizontalStrut(40));
        boxTelefono.add(jtfTelefono);
        JPanel panTelefono =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panTelefono.add(boxTelefono);
        
        Box boxAvatar =  Box.createHorizontalBox();
        avatar = new JLabel("Avatar");    
        boxAvatar.add(Box.createHorizontalStrut(10));
        boxAvatar.add(avatar);
        JPanel panAvatar =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panAvatar.add(boxAvatar);
        
        picture = new JLabel();   
        boxAvatar.add(Box.createHorizontalStrut(10));
        boxAvatar.add(picture);


        picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
        picture.setHorizontalAlignment(JLabel.CENTER);
        if(a.getAvatar() != null)
        {
        	cargarImagen(a.getAvatar(),a.getGenero());
        }
        else
        {
        	cargarImagen("Avatar_01","Hombre");
        }
        picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

        picture.setPreferredSize(new Dimension(177, 122+10));

        picture.setPreferredSize(new Dimension(177, 122+10));
       
        boxVCamposVentana = Box.createVerticalBox();
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panNombre);
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panApellidos);
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panDni);
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panEdad);
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panDireccion);
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panTelefono);
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panAvatar);
       
        
        boxVContenidoVerticalVentana.add(Box.createVerticalStrut(30));
        boxVContenidoVerticalVentana.add(boxVCamposVentana);
        boxVContenidoVerticalVentana.add(Box.createVerticalStrut(20));

        
        add(boxVContenidoVerticalVentana);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Box boxHBotones = Box.createHorizontalBox();
        
        aceptar = new JButton("Aceptar");
        aceptar.addActionListener(this);
        boxHBotones.add(aceptar);
        boxHBotones.add(Box.createHorizontalStrut(20));
        
        if(eleccion == "Administrador")
        {
	        mostrarAlumnos= new JButton("Mostrar Alumnos");
	        mostrarAlumnos.addActionListener(this);
	        boxHBotones.add(mostrarAlumnos);
	        boxHBotones.add(Box.createHorizontalStrut(40));
        }
        
        JPanel panBotones =  new JPanel(new FlowLayout(FlowLayout.RIGHT) );
        panBotones.add(boxHBotones);

        boxVContenidoVerticalVentana.add(Box.createVerticalStrut(20));
        boxVContenidoVerticalVentana.add(panBotones);
        
        setVisible(true);  
    }
    protected void cargarImagen(String name, String sexo) {
        
    	ImageIcon icon = null;

    	if(sexo.equals("Hombre"))
    	{
    		icon = createImageIcon("images/" +  name +"H" + ".jpg");
    	}
    	else
    	{
    		icon = createImageIcon("images/" +  name +"M" + ".jpg");
    	}
        picture.setIcon(icon);

        if (icon != null) {
            picture.setText(null);
        } else {
            picture.setText("Image not found");
        }
    }
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = FormularioAlumno.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    public void actionPerformed(ActionEvent e)
	{	
		if (e.getSource() instanceof JButton) 
		{
			JButton btnPulsado=(JButton)e.getSource();

			if (btnPulsado==aceptar) 
			{
				dispose();
			}		
			if(btnPulsado==mostrarAlumnos)
			{			
				volver = true;
				dispose();
			}
		}
	}
    public boolean volverVerAlumnos()
    {
    	return volver;
    }
}
